window.addEvent('load', function(e){
	/************************************************************************
	*************************BEGIN OBJECTS DEFINATION************************
	*************************************************************************/
	
	/***************************************************************
	****************************STEP 1******************************
	***************************************************************/
	var Step1 = new Class({
		//init value
		initialize : function(){
			this.panel_id = 'istep1';	
			//step 1: is completed or not
			this.flag = false;
			
			this.document_type = '';
			this.url = '';
			this.pid = 0;
			
			var _this = this;
			
			_this.stage_change(true);
			
			$(this.panel_id).addEvent('click', function(){
				_this.active(false);
			});
			
			_this.active(true);
			$(this.panel_id).addClass('step_clickable');
		},
		
		/**
		* check the flag value, if true
		* -get the remote document, check document type:
		* case: xml
		*	disable step2 regex and sub textbox
		*	disable step3 regex and sub textbox
		* 
		* go to the step 2
		*/
		next : function(){
			this.stage_change(false);
			
			//check the flag
			if(this.flag == false){
				alert('Please fill Link url');
				return false;
			}
			
			//get document type to decide to go to the step 2 or 4
			this._get_document_type(true);
		},
		
		/**
		* when url input box changed
		* -get url, plug_in and document_type value from url input field
		* -check the url is valid or not
		* set the flag value
		*/
		stage_change : function(connect){
			var url_regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
			this.url = trim($('link').value);
			this.pid = $('pid').value;
			this.flag = url_regexp.test(trim(this.url));
						
			//set tab style
			this.flag == true ? $(this.panel_id).addClass('step_clickable') : $(this.panel_id).removeClass('step_clickable');
			
			//first run get document type
			if(this.flag == true && connect == true){
				this._get_document_type(false);
			}
		},
		
		/**
		* get remote document by using Json.Remote, then set the document_type is http or xml
		* @next: boolen
		*/
		_get_document_type : function(next){
			var url = this.url;
			var _this = this;
			$('onloadingdata').setStyle('display', 'block');
			var time_number = new Date().getTime();
			var jSonRequest = new Json.Remote("index.php?option=com_yos_news_crawler&task=link.get_document_type&url="+ base64encode(url), {onComplete: function(document){
				$('onloadingdata').setStyle('display', 'none');
			    Step1.document_type = document.type;
			    
			    if(Step1.document_type == 'false'){
			    	alert('unable to connect');
			    	return;
			    }
			    
				Step2.recalculate();
				Step3.recalculate();
				Step5.recalculate();
				Step6.recalculate();
				
			    if(next == true){
					Step2.active(true);
			    }
				
			}}).send({'random': time_number});
		},
		
		/**
		* if force is false, check if the flag is true then active tab.
		* if force is true, just active tab.
		* set next button's value
		* hide back button
		*/
		active : function(force){
			Helper.active_tab(this.panel_id);
		}
	});
	
	/***************************************************************
	****************************STEP 2******************************
	***************************************************************/
	var Step2 = new Class({
	
		initialize : function(){
			this.panel_id = 'istep2';
			//step 2 is completed or not
			this.flag = false;
			
			this.getlink_reg = '';
			this.getlink_sub = 0;
			this.find_replace_links = '';
			
			this.links = Array();
			
			//to know when all test are done
			this.tests_done = 0;
									
			var _this = this;
			
			_this.stage_change();
			
			$(this.panel_id).addEvent('click', function(){
				_this.active(false);
			});
			
			$('testlink').addEvent('click', function(){
				_this.test(true);
			});
		},
		
		/**
		* check the flag value, if true go to the step 3
		*/
		next : function(){
			this.stage_change();
			
			if(this.flag == false){
				alert('Please check your input');
				return false;
			}
			
			Step3.active(true);
		},
		
		/**
		* DO NOT check the flag value
		* just go to the step 1
		*/
		back : function(){
			Step1.active(true);
		},
		
		/**
		* when input boxes getlink_reg and getlink_sub are changed, 
		* -update values
		* -check them are valid or not
		* set the flag value
		*/
		stage_change : function(){
			var regexp = /^\/(.+)\/[ismu]+/
			this.getlink_reg = trim($('getlink_reg').value);
			this.getlink_sub = parseInt($('getlink_sub').value);
			this.find_replace_links = $('find_replace_links').value;
			
			this.flag = true;
			if(!regexp.test(this.getlink_reg) && $('getlink_reg').disabled == false){
				this.flag = false;
			}
			if(this.getlink_sub < 0 && $('getlink_sub').disabled == false){
				this.flag = false;
			}
			
			this.flag == true ? $(this.panel_id).addClass('step_clickable') : $(this.panel_id).removeClass('step_clickable');
		},
		
		/**
		* if force is false, check if the flag is true then active tab.
		* if force is true, just active tab.
		* set next and back buttons's values, display them
		*/
		active : function(force){
			//called by human
			if(force == null || force == false){
				//check the flag
				if(this.flag == false)
					return false;
			}
			
			Helper.active_tab(this.panel_id);
		},
		
		/**
		* Test get links
		* @param: boolean display
		*/
		test : function(display){
			this.stage_change();
			if(this.flag == false){
				return false;
			}
									
			var _this = this;
			var display = display;
			
			if(display == true){
				$('step2_test').setHTML('');
				$('onloadingdata').setStyle('display', 'block');
			}
			
			var time_number = new Date().getTime();
			var jSonRequest = new Json.Remote("index.php?option=com_yos_news_crawler&task=link.step2_test", {onComplete: function(arr_links){
				
			    if(arr_links.result != 'true'){
			    	alert(arr_links.err_msg);
			    	return false;
			    }
			    
			    _this.links = arr_links.links.split('|');
			    
			    _this._test_links(display);
			    
			}}).send({'random':time_number+'', 
				'link': Helper.json_safe_string(Step1.url+''), 
				'document_type': Step1.document_type+'', 
				'getlink_reg': Helper.json_safe_string(Step2.getlink_reg)+'',
				'getlink_sub': Step2.getlink_sub+'',
				'find_replace_links': Helper.json_safe_string(Step2.find_replace_links).split('\n').join('|YOS_SEPARATOR|')+''});
		},
		
		/**
		* test all links received to make sure they are connect able and get the final redirect.
		* open one connection per link
		*/
		_test_links : function(display){
			var display = display;
			var _this = this;
			
			_this.tests_done = 0;
			for(var i = 0; i < _this.links.length; i++){
				var time_number = new Date().getTime();
				var jSonRequest = new Json.Remote("index.php?option=com_yos_news_crawler&task=link.step2_test_link", {onComplete: function(obj_link){
					_this.tests_done++;
					
					if(_this.tests_done == _this.links.length && display == true){
						$('onloadingdata').setStyle('display', 'none');
					}
					
				    if(obj_link.result == 'true'){
				   		_this.links[obj_link.index] = obj_link.link;
				    }
				    if(display == true){
				    	_this._show_test();
				    }
				}}).send({'random':time_number+'', 
					'index':i+'', 
					'link': Helper.json_safe_string(Step2.links[i]+'')
				});
			}			
		},
		
		_show_test : function(){
			var _this = this;
			var table_el = new Element('table', {'width': '100%'});
			var first_tr = new Element('tr');
			var th1 = new Element('th', {'width':'2%', 'align':'left'});
			th1.setHTML('#');
			th1.injectInside(first_tr);
			var th2 = new Element('th', {'width':'49%', 'align':'left'});
			th2.setHTML('Links');
			th2.injectInside(first_tr);
			var th3 = new Element('th', {'width':'49%', 'align':'left'});
			th3.setHTML('Raw codes');
			th3.injectInside(first_tr);
			first_tr.injectInside(table_el);
			for(var i = 0; i < _this.links.length; i++){
				var tr_el = new Element('tr');
				var td1 = new Element('td');
				td1.setHTML((i+1) + '');
				td1.injectInside(tr_el);
				var td2 = new Element('td');
				td2.setHTML('<a target="_blank" href="' + _this.links[i] + '">' + _this.links[i] + '</a>');
				td2.injectInside(tr_el);
				var td3 = new Element('td');
				td3.setText(_this.links[i] + '');				
				td3.injectInside(tr_el);
				tr_el.injectInside(table_el);
			}
			$('step2_test').setHTML('');
			table_el.injectInside('step2_test');
		},
		
		recalculate : function(){
			if(Step1.document_type == 'xml'){
				$('getlink_reg').disabled = true;
				$('getlink_sub').disabled = true;
			}
			else{
				$('getlink_reg').disabled = false;
				$('getlink_sub').disabled = false;
			}
			$('step2_test').setHTML('');
			
			this.stage_change();
		}
	});
	
	/***************************************************************
	****************************STEP 3******************************
	***************************************************************/
	var Step3 = new Class({
	
		initialize : function(){
			this.panel_id = 'istep3';
			//step 3 is completed or not
			this.flag = false;
			
			this.gettitle_reg = '';
			this.gettitle_sub = 0;
			this.find_replace_titles = '';
			
			this.titles = Array();
									
			var _this = this;
			
			_this.stage_change();
			
			$(this.panel_id).addEvent('click', function(){
				_this.active(false);
			});
			
			$('testtitle').addEvent('click', function(){
				_this.test(true);
			});
		},
		
		/**
		* check the flag value, if true go to the step 4
		*/
		next : function(){
			this.stage_change();			
			
			if(this.flag == false){
				alert('please check the input');
				return false;
			}
			
			Step4.active(true);
		},
		
		/**
		* DO NOT check the flag value
		* just go to the step 2
		*/
		back : function(){
			Step2.active(true);
		},
		
		/**
		* when input boxes gettitle_reg and gettitle_sub are changed, check them are valid or not
		* set the flag value
		*/
		stage_change : function(){
			var regexp = /^\/(.+)\/[ismu]*/
			this.gettitle_reg = $('gettitle_reg').value;
			this.gettitle_sub = parseInt($('gettitle_sub').value);
			this.find_replace_titles = $('find_replace_titles').value;
			
			this.flag = true;
			if(!regexp.test(this.gettitle_reg) && $('gettitle_reg').disabled == false){
				this.flag = false;
			}
			if(this.gettitle_sub < 0 && $('gettitle_sub').disabled == false){
				this.flag = false;
			}
			
			this.flag == true ? $(this.panel_id).addClass('step_clickable') : $(this.panel_id).removeClass('step_clickable');
		},
		
		/**
		* if force is false, check if the flag is true then active tab.
		* if force is true, just active tab.
		* set next and back buttons's values, display them
		*/
		active : function(force){
			//called by human
			if(force == null || force == false){
				//check the flag
				if(this.flag == false)
					return false;
			}
			
			Helper.active_tab(this.panel_id);
		},
		
		/**
		* Test get titles
		* @param: boolean display
		*/
		test : function(display){
			this.stage_change();
			if(this.flag == false){
				return false;
			}
									
			var _this = this;
			var display = display;
			
			$('onloadingdata').setStyle('display', 'block');
			if(display == true) $('step3_test').setHTML('');
			
			if(!Step2.links.length){
				Step2.test(false);
						
				var periodical;
				var wait_step2_test = (function() {
					if(Step2.links.length > 0 && Step2.tests_done == Step2.links.length){
						$clear(periodical);
						_this.test(display);
					}
				});
				periodical = wait_step2_test.periodical(500, this);
				return;
			}
			
			var time_number = new Date().getTime();
			var jSonRequest = new Json.Remote("index.php?option=com_yos_news_crawler&task=link.step3_test", {onComplete: function(arr_titles){
				$('onloadingdata').setStyle('display', 'none');
			    if(arr_titles.result != 'true'){
			    	alert(arr_titles.err_msg);
			    	return false;
			    }
			    
			    _this.titles = arr_titles.titles.split('|');
			    
			    if(display == true){
			    	_this._show_test();
			    }
			}}).send({'random':time_number+'', 
				'link': Helper.json_safe_string(Step1.url+''), 
				'document_type': Step1.document_type+'', 
				'gettitle_reg': Helper.json_safe_string(Step3.gettitle_reg)+'',
				'gettitle_sub': Step3.gettitle_sub+'',
				'find_replace_titles': Helper.json_safe_string(Step3.find_replace_titles).split('\n').join('|YOS_SEPARATOR|')+''});
		},
		
		_show_test : function(){
			var _this = this;
			var table_el = new Element('table', {'width': '100%'});
			var first_tr = new Element('tr');
			var th1 = new Element('th', {'width':'2%', 'align':'left'});
			th1.setHTML('#');
			th1.injectInside(table_el);
			var th2 = new Element('th', {'width':'49%', 'align':'left'});
			th2.setHTML('Links');
			th2.injectInside(table_el);
			var th3 = new Element('th', {'width':'49%', 'align':'left'});
			th3.setHTML('Raw codes');
			th3.injectInside(table_el);
			for(var i = 0; i < _this.titles.length; i++){
				var tr_el = new Element('tr');
				var td1 = new Element('td');
				td1.setHTML((i+1) + '');
				td1.injectInside(tr_el);
				var td2 = new Element('td');
				td2.setHTML('<a target="_blank" href="'+ Step2.links[i] +'">' + _this.titles[i] + '</a>');
				td2.injectInside(tr_el);
				var td3 = new Element('td');
				td3.setText(_this.titles[i] + '');				
				td3.injectInside(tr_el);
				tr_el.injectInside(table_el);
			}
			table_el.injectInside('step3_test');
		},
		
		recalculate : function(){
			if(Step1.document_type == 'xml'){
				$('gettitle_reg').disabled = true;
				$('gettitle_sub').disabled = true;
			}
			else{
				$('gettitle_reg').disabled = false;
				$('gettitle_sub').disabled = false;
			}
			$('step3_test').setHTML('');
			
			this.stage_change();
		}
	});
	
	/***************************************************************
	****************************STEP 4******************************
	***************************************************************/
	var Step4 = new Class({
	
		initialize : function(){
			this.panel_id = 'istep4';
			//step 4 is completed or not
			this.flag = false;
			
			this.dataway = 0;
									
			var _this = this;
			
			_this.stage_change();
			
			$(this.panel_id).addEvent('click', function(){
				_this.active(false);
			});
		},
		
		/**
		* check the flag value, if true
		* check the step1, 
		* if step1->document_type = xml and step4-dataway = 0 then go to step 6
		* else go to step 5
		*/
		next : function(){
			this.stage_change();
			
			if(this.flag == false){
				alert('please check the input');
				return false;
			}
			
			Step5.recalculate();
			Step5.active(true);
		},
		
		/**
		* DO NOT check the flag value
		* then check the Step1 document type. if type is xml then go to the step1, else go to the step3
		*/
		back : function(){
			Step3.active(true);
		},
		
		/**
		* when select box dataway is changed, check them are valid or not
		* set the flag value
		*/
		stage_change : function(){
			this.dataway = $('dataway').value;
			
			this.flag = true;
			if(this.dataway < 0){
				this.flag = false;
			}
						
			this.flag == true ? $(this.panel_id).addClass('step_clickable') : $(this.panel_id).removeClass('step_clickable');
		},
		
		/**
		* if force is false, check if the flag is true then active tab.
		* if force is true, just active tab.
		* set next and back buttons's values, display them
		*/
		active : function(force){
			//called by human
			if(force == null || force == false){
				//check the flag
				if(this.flag == false)
					return false;
			}
			
			Helper.active_tab(this.panel_id);
		}
	});
	
	/***************************************************************
	****************************STEP 5******************************
	***************************************************************/
	var Step5 = new Class({
	
		initialize : function(){
			this.panel_id = 'istep5';
			//step is completed or not
			this.flag = false;
			
			this.getintro_reg = '';
			this.getintro_sub = 0;
			this.cut_content = 0;
			this.numberword = 0;
			this.find_replace_intro = '';
			
			this.intros = Array();
			this.test_intros_done = 0;
			
			var _this = this;
			
			_this.stage_change();
			
			$(this.panel_id).addEvent('click', function(){
				_this.active(false);
			});
			
			$('testintro').addEvent('click', function(){
				_this.test(true);
			});
		},
		
		/**
		* check the flag value, if true
		* check the step1, 
		* if step1->document_type = xml and step4->dataway = 3, finish
		* if step4->dataway = 2, go to step 7
		* else go to step 6
		*/
		next : function(){
			this.stage_change();
			
			if(this.flag == false){
				alert('please check the input');
				return false;
			}
			
			if(Step1.document_type == 'xml' && Step4.dataway == 3){
//				Step6.destruct();
//				Step7.destruct();
				Finish.active(true);
				return;
			}
			
			if(Step4.dataway == 2){
//				Step6.destruct();
				Step7.active(true);
				return;
			}
			
			Step6.active(true);			
		},
		
		/**
		* DO NOT check the flag value
		* then check the Step1 document type. if type is xml then go to the step1, else go to the step3
		*/
		back : function(){
			Step4.active(true);
		},
		
		/**
		* when select box dataway is changed, check them are valid or not
		* set the flag value
		*/
		stage_change : function(){
			var regexp = /^\/(.+)\/[ismu]*/
			this.getintro_reg = trim($('getintro_reg').value);
			this.getintro_sub = parseInt($('getintro_sub').value);
			
			this.cut_content = parseInt(Helper.get_form_field('newsForm', 'cut_content'));
						
			this.numberword = parseInt($('numberword').value);
			this.find_replace_intro = trim($('find_replace_intro').value);			
			
			this.flag = true;
			if(!regexp.test(this.getintro_reg) && $('getintro_reg').disabled == false){
				this.flag = false;
			}
			if(this.getintro_sub < 0 && $('getintro_sub').disabled == false){
				this.flag = false;
			}			
			if(this.cut_content && this.numberword <= 0){
				this.flag = false;
			}
			
			this.flag == true ? $(this.panel_id).addClass('step_clickable') : $(this.panel_id).removeClass('step_clickable');
		},
		
		/**
		* if force is false, check if the flag is true then active tab.
		* if force is true, just active tab.
		* set next and back buttons's values, display them
		*/
		active : function(force){
			//called by human
			if(force == null || force == false){
				//check the flag
				if(this.flag == false)
					return false;
			}
			
			Helper.active_tab(this.panel_id);
		},
		
		/**
		* Test get intro text
		* @param: boolean display
		*/
		test : function(display){
			this.stage_change();
			if(this.flag == false){
				return false;
			}
									
			var _this = this;
			var display = display;
			
			if(display == true) $('step5_test').setHTML('');
			$('onloadingdata').setStyle('display', 'block');
			
			if(!Step2.links.length){
				Step2.test(false);
						
				var periodical;
				var wait_step2_test = (function() {
					if(Step2.links.length > 0 && Step2.tests_done == Step2.links.length){
						$clear(periodical);
						_this.test(display);
					}
				});
				periodical = wait_step2_test.periodical(500, this);
				return;
			}
			
			if(!Step3.titles.length){
				Step3.test(false);
						
				var periodical;
				var wait_step3_test = (function() {
					if(Step3.titles.length > 0){
						$clear(periodical);
						_this.test(display);
					}
				});
				periodical = wait_step3_test.periodical(500, this);
				return;
			}
			
			//build result table
			var table = new Element('table', {'width':'100%'});
			var first_tr = new Element('tr');
			var th1 = new Element('th', {'width':'65%', 'align':'left'});
			th1.setHTML('HTML mode');
			th1.injectInside(first_tr);
			var th2 = new Element('th', {'width':'35%', 'align':'left'});
			th2.setHTML('Raw codes');
			th2.injectInside(first_tr);
			first_tr.injectInside(table);
			for(var i = 0; i < Step2.links.length; i++){
				var tr1 = new Element('tr', {'class': 'test-block-title'});
				var td = new Element('td', {'colspan':'2'});
				var test_block_num = new Element('span', {'class': 'test-block-num'});
				test_block_num.setHTML('#' + (i + 1) + ' ');
				test_block_num.injectInside(td);
				
				var test_block_title = new Element('a', {
					'class': 'test-block-title',
					'target': '_blank',
					'href': decodeURIComponent(Step2.links[i])
				});
				test_block_title.setHTML(Step3.titles[i]);
				test_block_title.injectInside(td);
				td.injectInside(tr1);
				tr1.injectInside(table);
				
				var tr2 = new Element('tr', {'class': 'test-block-data'});
				var td1 = new Element('td', {'class': 'test-block-view'});
				td1.setProperty('id', 'step5-test-view-'+i);
				//td1.setHTML(_this.intros[i]);
				td1.injectInside(tr2);
				var td2 = new Element('td', {'class': 'test-block-raw'});
				td2.setProperty('id', 'step5-test-raw-'+i);
				//td2.setText(_this.intros[i]);
				td2.injectInside(tr2);
				tr2.injectInside(table);
			}
			if(display == true){
				$('step5_test').setHTML('');
				table.injectInside('step5_test');
			}
			
			if(Step4.dataway == 0 || Step4.dataway == 3){
				_this._test_all_links(display);
			}
			else{
				_this._test_each_link(display);
			}
		},
		
		_test_each_link : function(display){
			var display = display;
			var _this = this;

			_this.test_intros_done = 0;
			for(var i = 0; i < Step2.links.length; i++){
				var time_number = new Date().getTime();
				var jSonRequest = new Json.Remote("index.php?option=com_yos_news_crawler&task=link.step5_test_a_link", {onComplete: function(obj_intro){
					_this.test_intros_done++;
					
					if(obj_intro.result != 'true'){
						if(obj_intro.index == 'NaN'){
							return false;
						}
						_this.intros[obj_intro.index] = obj_intro.err_msg;
				    }				    				    
				    else{
				    	_this.intros[obj_intro.index] = obj_intro.intro;
				    }
				    
				    if(display == true){
				    	if(_this.test_intros_done == Step2.links.length){
				    		$('onloadingdata').setStyle('display', 'none');
				    	}
				    	
						$('step5-test-view-'+obj_intro.index).setHTML(_this.intros[obj_intro.index]);
						$('step5-test-raw-'+obj_intro.index).setText(_this.intros[obj_intro.index]);
				    }
				}}).send({'random':time_number+'', 
					'link': Helper.json_safe_string(Step2.links[i]+''),
					'index': i+'',
					'document_type': Step1.document_type+'', 
					'dataway': Step4.dataway+'',
					'getintro_reg': Helper.json_safe_string(Step5.getintro_reg)+'',
					'getintro_sub': Step5.getintro_sub+'',
					'cut_content': Step5.cut_content+'',
					'numberword': Step5.numberword+'',
					'find_replace_intro': Helper.json_safe_string(Step5.find_replace_intro).split('\n').join('|YOS_SEPARATOR|')+''});
			}
		},
		
		_test_all_links : function(display){
			var display = display;
			var _this = this;
			
			var arr_step2_links = Step2.links;
			for(var i = 0; i < Step2.links.length; i++){
				arr_step2_links[i] = Helper.json_safe_string(Step2.links[i]+'');
			}
			var str_step2_links = arr_step2_links.join('|');
			
			var time_number = new Date().getTime();
			var jSonRequest = new Json.Remote("index.php?option=com_yos_news_crawler&task=link.step5_test", {onComplete: function(arr_intros){
			    $('onloadingdata').setStyle('display', 'none');
				
				if(arr_intros.result != 'true'){
			    	alert(arr_intros.err_msg);
			    	return false;
			    }
			    
			    _this.intros = arr_intros.intros.split('|YOS_SEPARATOR|');
			    
			    if(display == true){
			    	for(var i = 0; i < _this.intros.length; i++){
						$('step5-test-view-'+i).setHTML(_this.intros[i]);
						$('step5-test-raw-'+i).setText(_this.intros[i]);
					}
			    }			    
			}}).send({'random':time_number+'', 
				'link': Helper.json_safe_string(Step1.url+''), 
				'document_type': Step1.document_type+'', 
				'links': str_step2_links+'',
				'dataway': Step4.dataway+'',
				'getintro_reg': Helper.json_safe_string(Step5.getintro_reg)+'',
				'getintro_sub': Step5.getintro_sub+'',
				'cut_content': Step5.cut_content+'',
				'numberword': Step5.numberword+'',
				'find_replace_intro': Helper.json_safe_string(Step5.find_replace_intro).split('\n').join('|YOS_SEPARATOR|')+''});			
		},
		
		recalculate : function(){
			if(Step1.document_type == 'xml'){
				switch(parseInt(Step4.dataway)){
					case 0:
						$('getintro_reg').disabled = true;
						$('getintro_sub').disabled = true;
						$('cut_content0').checked = true;
						$('cut_content0').disabled = true;
						$('cut_content1').disabled = true;
						$('numberword').disabled = true;
						break;
					case 1:
						$('getintro_reg').disabled = false;
						$('getintro_sub').disabled = false;
						$('cut_content0').checked = true;
						$('cut_content0').disabled = true;
						$('cut_content1').disabled = true;
						$('numberword').disabled = true;
						break;
					case 2:
						$('getintro_reg').disabled = false;
						$('getintro_sub').disabled = false;
						$('cut_content0').disabled = false;
						$('cut_content1').disabled = false;
						$('numberword').disabled = false;
						break;
					case 3:
						$('getintro_reg').disabled = true;
						$('getintro_sub').disabled = true;
						$('cut_content0').disabled = false;
						$('cut_content1').disabled = false;
						$('numberword').disabled = false;
						break;
				}
			}
			else {
				switch(parseInt(Step4.dataway)){
					case 0:
						$('getintro_reg').disabled = false;
						$('getintro_sub').disabled = false;
						$('cut_content0').checked = true;
						$('cut_content0').disabled = true;
						$('cut_content1').disabled = true;
						$('numberword').disabled = true;
						break;
					case 1:
						$('getintro_reg').disabled = false;
						$('getintro_sub').disabled = false;
						$('cut_content0').checked = true;
						$('cut_content0').disabled = true;
						$('cut_content1').disabled = true;
						$('numberword').disabled = true;
						break;
					case 2:
						$('getintro_reg').disabled = false;
						$('getintro_sub').disabled = false;
						$('cut_content0').disabled = false;
						$('cut_content1').disabled = false;
						$('numberword').disabled = false;
						break;
					case 3:
						$('getintro_reg').disabled = false;
						$('getintro_sub').disabled = false;
						$('cut_content0').disabled = false;
						$('cut_content1').disabled = false;
						$('numberword').disabled = false;
						break;
				}
			}
			
			this.stage_change();
		}
	});
	
	/***************************************************************
	****************************STEP 6******************************
	***************************************************************/
	var Step6 = new Class({
	
		initialize : function(){
			this.panel_id = 'istep6';
			//step is completed or not
			this.flag = false;
			
			this.getfull_reg = '';
			this.getfull_sub = 0;
			this.find_replace_full = '';
			
			this.fulls = Array();
			this.test_fulls_done = 0;
					
			var _this = this;
			
			_this.stage_change();
			
			$(this.panel_id).addEvent('click', function(){
				_this.active(false);
			});
			
			$('testfull').addEvent('click', function(){
				_this.test(true);
			});
		},
		
		/**
		* check the flag value, if true go to step 7
		*/
		next : function(){
			this.stage_change();
			
			if(this.flag == false){
				alert('please check the input');
				return false;
			}
			
			Step7.active(true);			
		},
		
		/**
		* DO NOT check the flag value
		* then check the Step1 document type. if type is xml then go to the step1, else go to the step3
		*/
		back : function(){
			if(Step1.document_type == 'xml' && Step4.dataway == 0){
				Step4.active(true);
				return;
			}
			
			Step5.active(true);
		},
		
		/**
		* when select box dataway is changed, check them are valid or not
		* set the flag value
		*/
		stage_change : function(){
			var regexp = /^\/(.+)\/[ismu]*$/
			this.getfull_reg = trim($('getfull_reg').value);
			this.getfull_sub = parseInt($('getfull_sub').value);
			this.find_replace_full = trim($('find_replace_full').value);
			
			this.flag = true;
			if(!regexp.test(this.getfull_reg) || this.getfull_sub < 0){
				this.flag = false;
			}
						
			this.flag == true ? $(this.panel_id).addClass('step_clickable') : $(this.panel_id).removeClass('step_clickable');
		},
		
		/**
		* if force is false, check if the flag is true then active tab.
		* if force is true, just active tab.
		* set next and back buttons's values, display them
		*/
		active : function(force){
			//called by human
			if(force == null || force == false){
				//check the flag
				if(this.flag == false)
					return false;
			}
			
			Helper.active_tab(this.panel_id);
		},
		
		/**
		* Test get fulltext
		*/
		test : function(display){
			this.stage_change();
			if(this.flag == false){
				return false;
			}
									
			var _this = this;
			var display = display;
			
			if(display == true){
				$('step6_test').setHTML('');
				$('onloadingdata').setStyle('display', 'block');
			}
			
			if(!Step2.links.length){
				Step2.test(false);
						
				var periodical;
				var wait_step2_test = (function() {
					if(Step2.links.length > 0 && Step2.tests_done == Step2.links.length){
						$clear(periodical);
						_this.test(display);
					}
				});
				periodical = wait_step2_test.periodical(500, this);
				return;
			}
			
			if(!Step3.titles.length){
				Step3.test(false);
						
				var periodical;
				var wait_step3_test = (function() {
					if(Step3.titles.length > 0){
						$clear(periodical);
						_this.test(display);
					}
				});
				periodical = wait_step3_test.periodical(500, this);
				return;
			}
			
			var table = new Element('table', {'width':'100%'});
			var first_tr = new Element('tr');
			var th1 = new Element('th', {'width':'65%', 'align':'left'});
			th1.setHTML('HTML mode');
			th1.injectInside(first_tr);
			var th2 = new Element('th', {'width':'35%', 'align':'left'});
			th2.setHTML('Raw codes');
			th2.injectInside(first_tr);
			first_tr.injectInside(table);
			for(var i = 0; i < Step2.links.length; i++){
				var tr1 = new Element('tr', {'class': 'test-block-title'});
				var td = new Element('td', {'colspan':'2'});
				var test_block_num = new Element('span', {'class': 'test-block-num'});
				test_block_num.setHTML('#' + (i + 1) + ' ');
				test_block_num.injectInside(td);
				
				var test_block_title = new Element('a', {
					'class': 'test-block-title',
					'target': '_blank',
					'href': decodeURIComponent(Step2.links[i])
				});
				test_block_title.setHTML(Step3.titles[i]);
				test_block_title.injectInside(td);
				td.injectInside(tr1);
				tr1.injectInside(table);
				
				var tr2 = new Element('tr', {'class': 'test-block-data'});
				var td1 = new Element('td', {'class': 'test-block-view'});
				td1.setProperty('id', 'step6-test-view-'+i);
				td1.injectInside(tr2);
				var td2 = new Element('td', {'class': 'test-block-raw'});
				td2.setProperty('id', 'step6-test-raw-'+i);
				td2.injectInside(tr2);
				tr2.injectInside(table);
			}
			
			if(display == true){
				$('onloadingdata').setStyle('display', 'block');
				$('step6_test').setHTML('');
				table.injectInside('step6_test');
			}
			
			_this.test_fulls_done = 0;
			for(var i = 0; i < Step2.links.length; i++){
				var time_number = new Date().getTime();
				var jSonRequest = new Json.Remote("index.php?option=com_yos_news_crawler&task=link.step6_test", {onComplete: function(obj_full){
				    _this.test_fulls_done++;
				    if(_this.test_fulls_done == Step2.links.length){
				    	$('onloadingdata').setStyle('display', 'none');
				    }

				    if(obj_full.result != 'true'){
				    	if(obj_full.index == 'NaN'){
				    		return false;
				    	}
				    	_this.fulls[obj_full.index] = obj_full.err_msg;
				    }
				    else{
				    	_this.fulls[obj_full.index] = obj_full.full;
				    }
				    
				    if(display == true){
				    	$('step6-test-view-'+obj_full.index).setHTML(_this.fulls[obj_full.index]);
				    	$('step6-test-raw-'+obj_full.index).setText(_this.fulls[obj_full.index]);
				    }
				}}).send({'random':time_number+'', 
					'link': Helper.json_safe_string(Step2.links[i]+''), 
					'document_type': Step1.document_type+'', 
					'index': i+'',
					'dataway': Step4.dataway+'',
					'getfull_reg': Helper.json_safe_string(_this.getfull_reg)+'',
					'getfull_sub': _this.getfull_sub+'',
					'find_replace_full': Helper.json_safe_string(_this.find_replace_full).split('\n').join('|YOS_SEPARATOR|')+''});
			}
		},
		
		_show_test : function(){
			var _this = this;
			
			var table = new Element('table', {'width':'100%'});
			var first_tr = new Element('tr');
			var th1 = new Element('th', {'width':'65%', 'align':'left'});
			th1.setHTML('HTML mode');
			th1.injectInside(first_tr);
			var th2 = new Element('th', {'width':'35%', 'align':'left'});
			th2.setHTML('Raw codes');
			th2.injectInside(first_tr);
			first_tr.injectInside(table);
			for(var i = 0; i < _this.fulls.length; i++){
				var tr1 = new Element('tr', {'class': 'test-block-title'});
				var td = new Element('td', {'colspan':'2'});
				var test_block_num = new Element('span', {'class': 'test-block-num'});
				test_block_num.setHTML('#' + (i + 1) + ' ');
				test_block_num.injectInside(td);
				
				var test_block_title = new Element('a', {
					'class': 'test-block-title',
					'target': '_blank',
					'href': decodeURIComponent(Step2.links[i])
				});
				test_block_title.setHTML(Step3.titles[i]);
				test_block_title.injectInside(td);
				td.injectInside(tr1);
				tr1.injectInside(table);
				
				var tr2 = new Element('tr', {'class': 'test-block-data'});
				var td1 = new Element('td', {'class': 'test-block-view'});
				td1.setHTML(_this.fulls[i]);
				td1.injectInside(tr2);
				var td2 = new Element('td', {'id': 'step6_test_' + i,'class': 'test-block-raw'});
				td2.setText(_this.fulls[i]);
				td2.injectInside(tr2);
				tr2.injectInside(table);
			}
			
			table.injectInside('step6_test');
		},
		
		recalculate : function(){
			switch(parseInt(Step4.dataway)){
				case 0:
				case 1:
					$('getfull_reg').disabled = false;
					$('getfull_sub').disabled = false;
					break;
				case 2:
				case 3:
					$('getfull_reg').disabled = true;
					$('getfull_sub').disabled = true;
					break;
			}
						
			this.stage_change();
		}
	});
	
	/***************************************************************
	****************************STEP 7******************************
	***************************************************************/
	var Step7 = new Class({
	
		initialize : function(){
			this.panel_id = 'istep7';
			//step is completed or not
			this.flag = false;
			
			this.has_multiple_pages = 0;
			this.multiple_way = 0;
			this.pages_detect_reg = '';
			this.pages_detect_sub = 0;
			
			this.subpages =  Array();
			
			var _this = this;
			
			_this.stage_change();
			
			$(this.panel_id).addEvent('click', function(){
				_this.active(false);
			});
			
			$('test_pages_detect').addEvent('click', function(){
				_this.test(true);
			});
		},
		
		/**
		* Check the flag then
		* if has_multiple_pages is fail, then finish
		* else go to the step 8
		*/
		next : function(){
			this.stage_change();
			
			if(this.flag == false){
				alert('please check the input');
				return false;
			}
			
			if(this.has_multiple_pages == 0){
//				Step8.destruct();
				Finish.active(true);
				return;
			}
			
			Step8.active(true);
		},
		
		/**
		* DO NOT check the flag value
		* then check the Step1 document type. if type is xml then go to the step1, else go to the step3
		*/
		back : function(){
			if(Step4.dataway == 2){
				Step5.active(true);
				return;
			}
			
			Step6.active(true);
		},
		
		/**
		* when select box dataway is changed, check them are valid or not
		* set the flag value
		*/
		stage_change : function(){
			var regexp = /^\/(.+)\/[ismu]*/
		
			this.has_multiple_pages = parseInt(Helper.get_form_field('newsForm', 'has_multiple_pages'));
			
			this.multiple_way = parseInt($('multiple_way').value);
			this.pages_detect_reg = trim($('pages_detect_reg').value);
			this.pages_detect_sub = parseInt($('pages_detect_sub').value);
			
			this.flag = true;
			if(this.has_multiple_pages == 1){
				if(!regexp.test(this.pages_detect_reg) || this.pages_detect_sub < 0){
					this.flag = false;
				}
			}
									
			this.flag == true ? $(this.panel_id).addClass('step_clickable') : $(this.panel_id).removeClass('step_clickable');
		},
		
		/**
		* if force is false, check if the flag is true then active tab.
		* if force is true, just active tab.
		* set next and back buttons's values, display them
		*/
		active : function(force){
			//called by human
			if(force == null || force == false){
				//check the flag
				if(this.flag == false)
					return false;
			}
			
			Helper.active_tab(this.panel_id);
		},
		
		/**
		* Test get fulltext
		*/
		test : function(display){
			this.stage_change();
			if(this.flag == false){
				alert('please check input');
				return false;
			}
			
			if(this.has_multiple_pages == 0){
				alert('Has multiple page is No');
				return false;
			}			
			
			//cannot get multiple page from link page or rss page.
			if(Step4.dataway == 3){
				alert('cannot get multiple page from link page or rss page');
				return false;
			}
			
			var _this = this;
			var display = display;
			
			if(display == true) $('step7_test').setHTML('');
			
			if(!Step2.links.length){
				Step2.test(false);
						
				var periodical;
				var wait_step2_test = (function() {
					if(Step2.links.length > 0){
						$clear(periodical);
						_this.test(display);
					}
				});
				periodical = wait_step2_test.periodical(500, this);
				return;
			}
			
			if(!Step3.titles.length){
				Step3.test(false);
						
				var periodical;
				var wait_step3_test = (function() {
					if(Step3.titles.length > 0){
						$clear(periodical);
						_this.test(display);
					}
				});
				periodical = wait_step3_test.periodical(500, this);
				return;
			}
			
			//foreach step2's links, we create a json connection to test.
			var table = new Element('table', {'width':'100%'});
			for(var i = 0; i < Step2.links.length; i++){
				var tr1 = new Element('tr', {'class': 'test-block-title'});
				var td = new Element('td');
				var test_block_num = new Element('span', {'class': 'test-block-num'});
				test_block_num.setHTML('#' + (i + 1) + ' ');
				test_block_num.injectInside(td);
				
				var test_block_title = new Element('a', {
					'class': 'test-block-title',
					'target': '_blank',
					'href': decodeURIComponent(Step2.links[i])
				});
				test_block_title.setHTML(Step3.titles[i]);
				test_block_title.injectInside(td);
				td.injectInside(tr1);
				tr1.injectInside(table);
				
				var tr2 = new Element('tr', {'class': 'test-block-data'});
				var td2 = new Element('td', {'id': 'step7_test_' + i,'class': 'test-block-raw'});
				td2.injectInside(tr2);
				tr2.injectInside(table);
				
				//load subpages
				var arr_index = i;
				td2.addClass('loading');
				var time_number = new Date().getTime();
				var jSonRequest = new Json.Remote("index.php?option=com_yos_news_crawler&task=link.step7_test", {onComplete: function(arr_subpages){
					$('step7_test_' + arr_subpages.index).removeClass('loading');					
				    if(arr_subpages.result != 'true'){
				    	_this.subpages[arr_subpages.index] = arr_subpages.err_msg;
				    	return false;
				    }
				    
				    _this.subpages[arr_subpages.index] = arr_subpages.subpages.split('|YOS_SEPARATOR|');
				    
				    if(display == true){
				    	_this._show_test(arr_subpages.index);
				    }
				}}).send({'random':time_number+'', 
					'link': Helper.json_safe_string(Step2.links[arr_index]+''), 
					'index': arr_index+'', 
					'multiple_way': _this.multiple_way+'',
					'pages_detect_reg': Helper.json_safe_string(_this.pages_detect_reg)+'',
					'pages_detect_sub': _this.pages_detect_sub+''});
			}
			table.injectInside('step7_test');
		},
		
		_show_test : function(index){
			var _this = this;
			for(var i = 0; i < _this.subpages[index].length; i++){
				var div = new Element('div');
				div.setHTML('<a target="_blank" href="' + _this.subpages[index][i] + '">' + _this.subpages[index][i] + '</a>');
				div.injectInside('step7_test_'+index);
			}
		},
		
		recalculate : function(){
			this.stage_change();
		}
	});
	
	/***************************************************************
	****************************STEP 8******************************
	***************************************************************/
	var select_link=0;
	var Step8 = new Class({
	
		initialize : function(){
			this.panel_id = 'istep8';
			//step is completed or not
			this.flag = false;
			
			
			this.multipage_title_reg = '';
			this.multipage_title_sub = 0;
			this.multipage_reg = '';
			this.multipage_sub = 0;
			this.find_replace_multipage = '';

			this.subpages =  Array();
			this.subtitles = {};
					
			var _this = this;
			
			_this.stage_change();
			
			$(this.panel_id).addEvent('click', function(){
				_this.active(false);
			});
			
			$("test_multipage").addEvent('click', function(){
				_this.show_select(true);
			});
		},
		
		/**
		* Check the flag then
		* go to the finish step
		*/
		next : function(){
			this.stage_change();
			
			if(this.flag == false){
				alert('please check the input');
				return false;
			}
			
			Finish.active(true);
		},
		
		/**
		* DO NOT check the flag value
		* just go to the step 7
		*/
		back : function(){
			Step7.active(true);
		},
		
		/**
		* when select box dataway is changed, check them are valid or not
		* set the flag value
		*/
		stage_change : function(){
			var regexp = /^\/(.+)\/[ismu]*/
			//TODO: get value here
			this.multipage_title_reg = trim($('multipage_title_reg').value);
			this.multipage_title_sub = parseInt($('multipage_title_sub').value);
			this.multipage_reg = trim($('multipage_reg').value);
			this.multipage_sub = parseInt($('multipage_sub').value);
			this.find_replace_multipage = trim($('find_replace_multipage').value);
			
			this.flag = true;
			if(!regexp.test(this.multipage_reg) || this.multipage_sub < 0){
				this.flag = false;
			}
			if(Step7.has_multiple_pages == 0){
				this.flag = false;
			}
									
			this.flag == true ? $(this.panel_id).addClass('step_clickable') : $(this.panel_id).removeClass('step_clickable');
		},
		
		/**
		* if force is false, check if the flag is true then active tab.
		* if force is true, just active tab.
		* set next and back buttons's values, display them
		*/
		active : function(force){
			//called by human
			if(force == null || force == false){
				//check the flag
				if(this.flag == false)
					return false;
			}
			
			Helper.active_tab(this.panel_id);
		},
		
		test_multipage: function(display,sub_link){
			
			var subpages=Step7.subpages[sub_link];			
			var _this = this;
			
			var table = new Element('table', {'width':'100%'});
			for(var i = 0; i < subpages.length; i++){
				var tr1 = new Element('tr', {'class': 'test-block-title'});
				var td = new Element('td');
				var test_block_num = new Element('span', {'class': 'test-block-num'});
				test_block_num.setHTML('#' + (i + 2) + ' ');
				test_block_num.injectInside(td);
				
				var test_block_title = new Element('a', {
					'class': 'test-block-title',
					'target': '_blank',
					'href': decodeURIComponent(subpages[i])
				});
				test_block_title.setHTML(subpages[i]);
				test_block_title.injectInside(td);
				td.injectInside(tr1);
				tr1.injectInside(table);
				
				var tr2 = new Element('tr', {'class': 'test-block-data'});
				var td2 = new Element('td', {'id': 'step8_test_' + i,'class': 'test-block-raw'});
				td2.injectInside(tr2);
				tr2.injectInside(table);
				
				//load subpages
				var arr_index = i;
				td2.addClass('loading');
						
				var time_number = new Date().getTime();
				var jSonRequest = new Json.Remote("index.php?option=com_yos_news_crawler&task=link.step8_test", {onComplete: function(obj_subpage){
					$('step8_test_' + obj_subpage.index).removeClass('loading');
				    if(obj_subpage.result != 'true'){
				    	_this.subtitles[obj_subpage.index] = obj_subpage.err_msg;
				    	_this.subpages[obj_subpage.index] = obj_subpage.err_msg;
				    	return false;
				    }
				    _this.subpages[obj_subpage.index] = obj_subpage.subpage;
				    _this.subtitles[obj_subpage.index] = obj_subpage.subtitle;
				    
				    if(display == true){
				    	_this._show_test(obj_subpage.index);
				    }
				}}).send({'random':time_number+'', 
					'link': Helper.json_safe_string(Step2.links[sub_link]+''), 
					'index': arr_index+'', 					
					'multipage_title_reg': Helper.json_safe_string(_this.multipage_title_reg)+'',
					'multipage_title_sub': _this.multipage_title_sub+'',
					'multipage_reg': Helper.json_safe_string(_this.multipage_reg)+'',
					'multipage_sub': _this.multipage_sub+'',
					'find_replace_multipage': Helper.json_safe_string(_this.find_replace_multipage).split('\n').join('|YOS_SEPARATOR|')+'',
					'sub_link': Helper.json_safe_string(subpages[i])+''});
			}
			table.injectInside('step8_test');
			
		},
		
		_show_test : function(index){
			var _this = this;
			
			var index = index;
			
			var table = new Element('table', {'width':'100%'});
			
			var tr1 = new Element('tr', {'class': 'test-block-subtitle'});
			var td1 = new Element('td', {'width':'50%', 'class': 'test-block-view'});
			td1.setHTML(_this.subtitles[index]);
			td1.injectInside(tr1);
			tr1.injectInside(table);
			var td2 = new Element('td', {'width':'50%', 'class': 'test-block-raw'});
			td2.setText(_this.subtitles[index]);
			td2.injectInside(tr1);
			tr1.injectInside(table);
			
			var tr2 = new Element('tr', {'class': 'test-block-subpage'});
			var td1 = new Element('td', {'class': 'test-block-view'});
			td1.setHTML(_this.subpages[index]);
			td1.injectInside(tr2);
			var td2 = new Element('td', {'id': 'step8_test_raw_' + index,'class': 'test-block-raw'});
			td2.setText(_this.subpages[index]);
			td2.injectInside(tr2);
			tr2.injectInside(table);
			
			table.injectInside('step8_test_' + index);
		},
		
		/**
		* Show a select box of list available multiple page content.
		*/
		show_select:function(display)
		{			
			this.stage_change();
			
			if(this.flag == false){
				alert('please check the input');
				return false;
			}
			var _this = this;
			
			var display = display;				
			
			var bool=false;
			var i=0;
				
			if(!Step2.links.length){
				alert('Need to test first step 7');
				Step7.active(true);
				return false;			
			}
			var arrAllow=new Array();
			var j=0;
			var url_regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
			for(var i = 0; i < Step2.links.length; i++){
				if(!Step7.subpages[i]){
					continue;
				}
				
				if(!url_regexp.test(Step7.subpages[i][0])){
					continue;
				}
				
				arrAllow[j]=i;
				j=j+1;					
			}
			if(!arrAllow.length){
				alert('Need to test first step 7 #2');
				Step7.active(true);
				return false;			
			}	
			
			divcontain=$('selectsublink');
			divcontain.setHTML('');
			
			divcontain.setStyle('display', 'block');
			divcontain.setStyle('z-index', '65556');
			var select=new Element('select',{
				'name':'selectLink',
				'id':'selectLink',
				'styles':{
					'float':'left'
				}						
			});

			for(i=0;i<arrAllow.length;i++)
			{
				index=	arrAllow[i];
				var opt=new Element('option',{'value':index});						
				if(select_link==index)
					opt.selected=true;
				opt.innerHTML=Step3.titles[index];
				opt.injectInside(select);
			}			
			select.injectInside(divcontain);
			btn_continue=new Element('input',{						
				'type':'button',
				'value':'continue',
				'styles':{
					'margin':'0 0 0 5px'
				},
				'events': {
						'click': function(){										
								divcontain.setStyle('display', 'none');
								if(display == true) $('step8_test').setHTML('');
								divcontain.setHTML('');	
								select_link=select.value;
								_this.test_multipage(display,select.value);								
							}
					}
			});
			btn_continue.inject(divcontain,'bottom');
			btn_cancel=new Element('input',{						
				'type':'button',
				'value':'cancel',
				'styles':{
					'margin':'0 0 0 5px'
				},
				'events': {
						'click': function(){										
								divcontain.setStyle('display', 'none');									
								divcontain.setHTML('');											
							}
					}
			});
			btn_cancel.inject(divcontain,'bottom');			
		},
		
		recalculate : function(){
			this.stage_change();
		}
	});
	
	/***************************************************************
	****************************STEP 8******************************
	***************************************************************/
	var Finish = new Class({
	
		initialize : function(){
			this.panel_id = 'ifinish';
			//step is completed or not
			this.flag = false;
			
			var _this = this;
			
			_this.stage_change();
			
			$(this.panel_id).addEvent('click', function(){
				_this.active(false);
			});
		},
		
		/**
		* Check the flag then
		* go to the finish step
		*/
		next : function(){
			return false;
		},
		
		/**
		* DO NOT check the flag value
		*/
		back : function(){
			if(Step1.document_type == 'xml' && Step4.dataway == 3){
				Step5.active(true);
				return;
			}
			
			if(Step7.has_multiple_pages == 0){
				Step7.active(true);
				return;
			}
			
			Step8.active(true);
		},
		
		/**
		* when select box dataway is changed, check them are valid or not
		* set the flag value
		*/
		stage_change : function(){
			var regexp = /^\/(.+)\/[ismu]*/
			//TODO: get value here
			
			this.flag = true;	
			
									
			this.flag == true ? $(this.panel_id).addClass('step_clickable') : $(this.panel_id).removeClass('step_clickable');
		},
		
		/**
		* if force is false, check if the flag is true then active tab.
		* if force is true, just active tab.
		* set next and back buttons's values, display them
		*/
		active : function(force){
			//called by human
			if(force == null || force == false){
				//check the flag
				if(this.flag == false)
					return false;
			}
			
			Helper.active_tab(this.panel_id);
		}
	});
	
	/***************************************************************
	**************************HELPER CLASS**************************
	***************************************************************/
	var Steps_helper = new Class({
		
		initialize : function(){
//			this.arr_tab_ids = ['istep1','istep2','istep3','istep4','istep5','istep6','istep7','istep8','ifinish'];
		},
	
		_hide_nextbtn 		: function(){	$('toolbar-next').setStyle('display', 'none'); },
		
		_show_nextbtn 		: function(){	$('toolbar-next').setStyle('display', 'table-cell'); },
		
		_hide_backbtn 		: function(){	$('toolbar-back').setStyle('display', 'none'); },
		
		_show_backbtn 		: function(){	$('toolbar-back').setStyle('display', 'table-cell'); },
		
		_hide_savebtn 		: function(){	$('toolbar-save').setStyle('display', 'none'); },
		
		_show_savebtn 		: function(){	$('toolbar-save').setStyle('display', 'table-cell'); },
		
		_hide_applybtn 		: function(){	$('toolbar-apply').setStyle('display', 'none');	},
		
		_show_applybtn 		: function(){	$('toolbar-apply').setStyle('display', 'table-cell'); },
		
		_hide_previewbtn 	: function(){	$('toolbar-Preview').setStyle('display', 'none'); },
		
		_show_previewbtn 	: function(){	$('toolbar-Preview').setStyle('display', 'table-cell'); },		
		
		btn_process : function(tab_id){
			switch(tab_id){
				case 'istep1':
					this._hide_backbtn();
					this._show_nextbtn();
					this._hide_savebtn();
					this._hide_previewbtn();
					this._hide_applybtn();
					break;
				case 'istep2':
				case 'istep3':
				case 'istep4':
				case 'istep5':
				case 'istep6':
				case 'istep7':
				case 'istep8':
					this._show_backbtn();
					this._show_nextbtn();
					this._hide_savebtn();
					this._hide_previewbtn();
					this._hide_applybtn();
					break;				
				case 'ifinish':
					this._show_backbtn();
					this._hide_nextbtn();
					this._show_savebtn();
					this._show_previewbtn();
					this._show_applybtn();
					break;
			}
		},
		
		get_active_tab : function(){
			var obj_return;
			$('pane').getChildren().each(function(el){
				if(el.hasClass('open')){
					switch(el.getProperty('id')){
						case 'istep1':
							obj_return = Step1;
							break;
						case 'istep2':
							obj_return = Step2;
							break;
						case 'istep3':
							obj_return = Step3;
							break;
						case 'istep4':
							obj_return = Step4;
							break;
						case 'istep5':
							obj_return = Step5;
							break;
						case 'istep6':
							obj_return = Step6;
							break;
						case 'istep7':
							obj_return = Step7;
							break;
						case 'istep8':
							obj_return = Step8;
							break;
						case 'ifinish':
							obj_return = Finish;
							break;
					}
				}
			});
			return obj_return;
		},
		
		//active a tab, hide all others
		active_tab : function(tab_id){
			//get all tabs
			var tabs_title = $('pane').getChildren();
			var tabs_content = $('pane').getNext().getChildren();
			for(var i = 0; i < tabs_title.length; i++){
				if(tabs_title[i].getProperty('id') == tab_id){
					tabs_title[i].removeClass('closed');
					tabs_title[i].addClass('open');
					this.btn_process(tab_id);
					for(var j = 0; j < tabs_content.length; j++){
						if(j == i){
							tabs_content[j].setStyle('display', 'block');
						}
						else {
							tabs_content[j].setStyle('display', 'none');
						}
					}
				}
				else {
					tabs_title[i].removeClass('open');
					tabs_title[i].addClass('closed');
				}
			}
		},
		
		remove_tabs_click_event : function(){
			$('pane').getChildren().each(function(item){
				$(item).removeEvents('click');
			});
		},
		
		get_form_field : function(form_id, field_name){
			var return_value;
			$(form_id).toQueryString().split('&').each(function(el){
			    var arr_el = el.split('=');
			    if(arr_el[0] == field_name){
			        return_value = arr_el[1];
			    }
			});
			return return_value;
		},
		
		json_safe_string : function(str_in){
			var str_out = str_in;
			str_out = str_out.replace(/\+/g, encodeURIComponent('+'));
			str_out = str_out.replace(/&/g, encodeURIComponent('&'));
			return str_out;
		}
	});
	
	/************************************************************************
	*************************END OBJECTS DEFINATION**************************
	*************************************************************************/
	
	var Helper = new Steps_helper();
	
	//Remove all jtab click
	Helper.remove_tabs_click_event();
	
	var Step1 = new Step1();
	var Step2 = new Step2();
	var Step3 = new Step3();
	var Step4 = new Step4();
	var Step5 = new Step5();
	var Step6 = new Step6();
	var Step7 = new Step7();
	var Step8 = new Step8();
	var Finish = new Finish();

	//handle next button click
	$('toolbar-next').getFirst().addEvent('click', function(e){
		var open_tab = Helper.get_active_tab();
		open_tab.next();
	});
	//handle back button click
	$('toolbar-back').getFirst().addEvent('click', function(e){
		var open_tab = Helper.get_active_tab();
		open_tab.back();
	});
	var index_preview=0;
	$('toolbar-preview').addEvent('click', function(e) {
		$('yos-content').empty();	
		
		
		var title 		=	new Element('h2');		
		var select 		=	new Element('select');		
		if (Step2.links && Step2.links.length && Step3.titles && Step3.titles.length) {
			title.setHTML('<a href="'+Step2.links[index_preview]+'" target="_blank">'+Step3.titles[index_preview]+'</a>');
			
			select=new Element('select',{
				'name':'selectLink',
				'id':'selectLink',
				'styles':{
					'float':'right'
				},
				'events': {
						'change': function(){										
								index_preview=select.value;
								$('toolbar-preview').fireEvent('click');
						}
				}	
			});

			for(i=0;i<Step3.titles.length;i++)
			{				
				var opt=new Element('option',{'value':i});
				if(index_preview==i)
					opt.selected=true;				
				opt.innerHTML=Step3.titles[i];
				opt.injectInside(select);
			}
			select.inject($('yos-content'));
			
			br=new Element('br');
			hr=new Element('hr');
			
			br.inject(select,'after');
			hr.inject(br,'after');		
			
		}else{
			title.setHTML('Need to run test');
		}		
		var introtext	=	new Element('div', {'class': 'introtext'});
		if (Step5.intros && Step5.intros.length) {
			introtext.setHTML(Step5.intros[index_preview]);
		}
		var fulltext	=	new Element('div', {'class': 'fulltext'});
		if (Step6.fulls && Step6.fulls.length) {
			fulltext.setHTML(Step6.fulls[index_preview]);
		}	
		title.inject($('yos-content'));
		introtext.inject($('yos-content'));
		fulltext.inject($('yos-content'));
		
		$('yos-overlay').setStyle('display', '');
		$('yos-window').setStyle('display', '');
	});
	$('yos-overlay').addEvent('click', function(e) {
		$('yos-overlay').setStyle('display', 'none');
		$('yos-window').setStyle('display', 'none');
	});
	
	/************************************************************************
	**********************data way options (step 4)**************************
	************************************************************************/
	$('dataway').getChildren('option').each(function(option, index) {
		option.addEvent('mousemove', function(e){
			switch (option.getProperty('value')){
				case '0':
					$('supportway').setHTML('Get Introtext on Main-page and Get Fulltext on Detail-page <br/><img src="components/com_yos_news_crawler/assets/images/option1.png" />');
					break;
				case '1':
					$('supportway').setHTML('Get Introtext and Fulltext on Detail-page <br/><img src="components/com_yos_news_crawler/assets/images/option2.png" />');
					break;
				case '2':
					$('supportway').setHTML('Get Introtext on Detail-page. You can cut text to Full <br/><img src="components/com_yos_news_crawler/assets/images/option3.png" />');
					break;
				case '3':
					$('supportway').setHTML('Get Introtext on Main-page. You can cut text to Full <br/><img src="components/com_yos_news_crawler/assets/images/option4.png" />');
					break;
			}			
		});
		
		option.addEvent('mouseout', function(e){			
			switch (document.adminForm.dataway.value){
				case '0':
					$('supportway').setHTML('Get Introtext on Main-page and Get Fulltext on Detail-page <br/><img src="components/com_yos_news_crawler/assets/images/option1.png" />');
					break;
				case '1':
					$('supportway').setHTML('Get Introtext and Fulltext on Detail-page <br/><img src="components/com_yos_news_crawler/assets/images/option2.png" />');
					break;
				case '2':
					$('supportway').setHTML('Get Introtext on Detail-page. You can cut text to Full <br/><img src="components/com_yos_news_crawler/assets/images/option3.png" />');
					break;
				case '3':
					$('supportway').setHTML('Get Introtext on Main-page. You can cut text to Full <br/><img src="components/com_yos_news_crawler/assets/images/option4.png" />');
					break;
			}
		});
	});
});
function isArray(obj) {
//returns true is it is an array
	if (obj.constructor.toString().indexOf("Array") == -1)
		return false;
	else
		return true;
}